package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.AllTypeViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.PrepayResponse;
import sz.szsmk.citizencard.model.network.bean.response.ServiceSearchRes;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.ShowImageUtils;
import sz.szsmk.citizencard.util.StringUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/12 11:37
 * description:
 */
public class AllTypeAdapter extends BaseRecyclerAdapter<AllTypeAdapter.AllTypeHolder> {
    private Context mContext;

    private String type = "";
    private List<PrepayResponse.WebsiteListInfoBean> listBean = new ArrayList<>();

    private String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529572813554&di=a40a0385f7e0524dd2ceca88c93aba08&imgtype=0&src=http%3A%2F%2Fp1.qhimgs4.com%2Ft01a0c0dd4947f07eb5.jpg";
    private List<ServiceSearchRes.WebsiteListInfoBean> serviceData;

    public AllTypeAdapter(Context context) {

        mContext = context;
    }

    public AllTypeAdapter(Context context, String type) {
        //type "life" 生活页面的三个
        mContext = context;
        this.type = type;
    }

//    @Override
//    public int getItemViewType(int position) {
//        return R.layout.all_type_view;
//    }

    @Override
    public AllTypeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return AllTypeHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(AllTypeHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, AllTypeViewBinding binding) {
        Log.d("clark", "position=" + position);
        ShowImageUtils.showImageViewToCircle(DaggerApplication.getIns(), R.drawable.zhanweitu, url, binding.sev);
        if (listBean != null){
            PrepayResponse.WebsiteListInfoBean websiteListInfoBean = listBean.get(position);
            binding.alltypeShopname.setText(listBean.get(position).getShopName());
            int i = websiteListInfoBean.getDistance() / 10;
            String formatPrice = StringUtils.decimalFormatPrice(i);
            binding.alltypeDistance.setText(formatPrice + "km");
            binding.alltypeAdress.setText(listBean.get(position).getAddress()+"");
            ShowImageUtils.showImageViewToCircle(DaggerApplication.getIns(), R.drawable.zhanweitu,listBean.get(position).getPicUrl(),binding.sev);
            final double latitude = listBean.get(position).getLatitude();
            final double longitude = listBean.get(position).getLongitude();
            final int id = listBean.get(position).getId();
            binding.allTypeRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id",id);
                    bundle.putDouble("latitude",latitude);
                    bundle.putDouble("longitude",longitude);
                    JumpUtils.jumptoNatvie(mContext,"netdetail",0,bundle);
                }
            });

        }

    }

    @Override
    public int getItemCount() {
        if (type.equals("life")) {
            if (listBean.size() <= 3) {
                return listBean.size();
            } else {
                return 3;
            }
        } else {
            return null == listBean ? 0 : listBean.size();
        }
        // TODO: 三元运算符
//        return 3;
    }

    public void setTypeData(List<PrepayResponse.WebsiteListInfoBean> websiteListInfo,int addType) {
        if(addType==0){
            listBean.clear();
            listBean.addAll(websiteListInfo);
        }else if(addType==1){
            listBean.addAll(websiteListInfo);
        }
        notifyDataSetChanged();
    }

    public static class AllTypeHolder extends BaseHolder {
        private AllTypeViewBinding binding;

        public static AllTypeHolder creat(ViewGroup parent) {
            AllTypeViewBinding binding = AllTypeViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);

            return new AllTypeHolder(binding);
        }


        public AllTypeHolder(AllTypeViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
